<template>
	<view>
		<view>当前可提现余额:{{nowMoney}}元</view>
		<input placeholder="请填写提现金额" v-model="moeny" type="digit" step="0.01" min="0" @input="validateInput"/>
		<button @click="withdrawalClick">提现</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				moeny:'',
				nowMoney:100
			}
		},
		methods: {
			//提现
			async withdrawalClick(){
				if(this.moeny==''){
					uni.showToast({
						title: "请先填写提现金额",
						icon: "none"
					})
					
				}else{
					if(this.moeny>this.nowMoney){
						uni.showToast({
							title: "提现金额不可超过可提现金额",
							icon: "error"
						})
						return
					}
					const data = {
					    amount: this.moeny,
					    type: "weixin"
					  }
					  await takeMoney(data) 
					  .then(result => {  
					   // 成功
					    this.nowMoney = this.nowMoney - this.moeny
					    this.moeny = ''
					      uni.showToast({  
					      title: '申请提现成功',  
					      icon: 'success',  
					      duration: 1000  
					      });  
					  })  
					  .catch(error => {  
					      // 失败 
					    this.moeny = ''
					      uni.showToast({  
					      title: '申请提现失败',  
					      icon: 'none',  
					      duration: 2000  
					      });  
					  })  
				}
			},
			// 对输入的金额做处理
			validateInput(e) {
			  let inputValue = e.detail.value;
			  let integerPart = parseInt(inputValue);  // 整数
			  let decimalPart = inputValue - parseInt(inputValue);  // 小数
			  // 移除非数字和小数点以外的字符  
			  inputValue = inputValue.replace(/[^0-9.]/g, ''); 
			  // 小数点只能输入两位小数，并去除多余的  
			  if (inputValue.includes('.')) { 
			    if (inputValue.indexOf('.') === inputValue.length - 1) {
			      decimalPart = '.'
			    } else if (inputValue.indexOf('.') === inputValue.length - 2) {  
			      if (decimalPart == 0) {
			        decimalPart = '.0'
			      } else {
			        decimalPart = parseFloat(decimalPart);
			      }
			    } else {
			      decimalPart = inputValue.substr(inputValue.indexOf('.') + 1, 2);
			      decimalPart = parseFloat(decimalPart / 100)
			      console.log(decimalPart)
			    } 
			  }
			  // 整数部分补0，只针对第一位数字为0的情况  
			  if (inputValue.length === 1 && inputValue === '0') {  
			    inputValue = '';  
			    console.log(integerPart)
			  }  else if (inputValue[0] === '.') {  
			    integerPart = ''
			    console.log(integerPart, decimalPart, inputValue)
			  } else if (inputValue[0] !== '.') {   
			    inputValue = inputValue
			    console.log(integerPart, decimalPart, inputValue)
			    if (integerPart[0] === '0' && integerPart.length >= 1) {  
			      integerPart = integerPart.substr(1);  
			      console.log(integerPart)
			    }  
			  }  
			  // // 整数部分补0，只针对没有其他整数的情况  
			  if (integerPart === '' && decimalPart === '') {  
			    integerPart = '0';  
			  } else if (integerPart === '' && decimalPart !== '') {  
			    integerPart = 0;  
			  }
			  // 如果输入的值大于余额，则强制转换为余额值  
			  if (parseFloat(inputValue) > this.amount) {  
			    inputValue = this.amount;  
			  } else {  
			    console.log(integerPart, decimalPart)
			    inputValue = integerPart + decimalPart; // 重新组合整数部分和小数部分，并更新v-model的值  
			  }  
			 
			  this.$nextTick(() => {
			    console.log(inputValue)
			    this.moeny = inputValue 
			  }); 
			},
			
		}
	}
</script>

<style>

</style>
